import { getDicts } from "@/api/dict";
import { useDictStoreHook } from "@/store/modules/dict";
import { ToRefs } from "vue";
const { getDict, setDict } = useDictStoreHook();

/**
 * 获取字典数据
 */
export function useDict(...args: string[]): ToRefs<DictType> {
  const res: Ref<DictType> = ref({});
  return (() => {
    args.forEach((typeCode, index) => {
      res.value[typeCode] = [];
      const dicts = getDict(typeCode);
      if (dicts) {
        res.value[typeCode] = dicts;
      } else {
        getDicts(typeCode).then(({ data }) => {
          res.value[typeCode] = data;
          setDict(typeCode, res.value[typeCode]);
        });
      }
    });

    return toRefs(res.value);
  })();
}
